﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    public class T_OperationDal
    {
        static SGArchiveSystem7xEntities conn;

        /// <summary>
        /// 发布运营
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Release_Operation(T_ArchiveAj en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if(conn.T_ArchiveAj.Select(c=>c).Where(c=>c.DH == en.DH).Count()>0)
                    return _ReturnValue.Already;
                else
                {
                    conn.T_ArchiveAj.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch (Exception ex)
            {
                return _ReturnValue.Err;
            }
        }

        /// <summary>
        /// 添加运营
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Add_Operation(T_YYFILE en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                ///不能有重复的归档号
                if (conn.T_YYFILE.Select(c => c).Where(c => c.YY_BH == en.YY_BH).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    conn.T_YYFILE.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch (Exception ex)
            {
                return _ReturnValue.Err;
            }
        }

        /// <summary>
        /// 修改运营
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_Operation(T_YYFILE en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_YYFILE.Select(c => c).Where(c => c.YY_BH == en.YY_BH && c.YY_ID != en.YY_ID).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    var query = conn.T_YYFILE.Select(c => c).Where(c => c.YY_ID == en.YY_ID);
                    foreach (var n in query)
                    {
                        if (!SGArchiveSystem7xCommons.SGEntity.ModifyEntity(n, en, "YY_ID"))
                            return _ReturnValue.Err;
                    }
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch (Exception ex)
            {
                return _ReturnValue.Err;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="start">分页的起始行，-1 代表不分页</param>
        /// <param name="row">每页显示数， -1 代表不分页</param>
        /// <param name="count">总数</param>
        /// <returns></returns>
        public static List<T_YYFILE> Get_Operation(out int count, string dh, string stime, string etime, int status = -1, int start = -1, int row = -1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_YYFILE.Select(c => c);
                if (dh != "" && dh != null)
                {
                    query = query.Where(c => c.YY_BH == dh);
                }
                if (status > -1)
                {
                    query = query.Where(c => c.YY_STATUS == status);
                }
                if (stime != null && stime != "")
                {
                    DateTime st = DateTime.Parse(stime);
                    query = query.Where(c => c.YY_DATE >= st);
                }
                if (etime != null && etime != "")
                {
                    DateTime et = DateTime.Parse(etime);
                    query = query.Where(c => c.YY_DATE <= et);
                }
                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderBy(c => c.YY_ID).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.ToList();
                    count = list.Count;
                    return list;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取当个数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T_YYFILE Get_OperationId(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_YYFILE.Select(c => c).Where(c => c.YY_ID == id).Single();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 上传PDF
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pdfname"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static int Upload(int id, string pdfname, V_User user)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_YYFILE.Select(c => c).Where(c => c.YY_ID == id);
                foreach (var n in query)
                {
                    n.YY_DZWZ = pdfname;
                    n.YY_DZWZUSERID = user.U_ID;
                    n.YY_DZWZUSERNAME = user.U_NAME;
                    n.YY_DZWZDATE = DateTime.Now;
                }
                conn.SaveChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Del_Operation(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var qurey = conn.T_YYFILE.Select(c => c).Where(c => c.YY_ID == id);
                foreach (var n in qurey)
                {
                    conn.T_YYFILE.Remove(n);
                }
                conn.SaveChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        
    }
}
